import useSWR from "swr";
import axios from "axios";

export const getWeather = async (params: { city: string }) => {
  const res = await axios.get(`lolimi/API/weather/api.php?city=${params.city}`);
  return res.data;
};

export function useWeather(params: { city: string }) {
  const key = { path: "/weather", params, method: "get" };
  const result = useSWR(key, () => getWeather(params), {
    revalidateIfStale: false, //智能控制： 只在 key 不变且缓存中有数据时才不重新请求。如果 key 发生变化，useSWR 也会正常发起新的请求。
  });
  return result;
}
